package com.jlkf.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jlkf.common.redis.service.JedisClient;
import com.jlkf.dao.CarCaculationMapper;
import com.jlkf.pojo.CarCaculation;
import com.jlkf.service.CarCaculationService;
import com.platform.server.response.response.pojo.ReturnData;
import com.platform.server.util.StringTools;

import net.sf.json.JSONArray;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
@Service
public class CarCaculationServiceImpl implements CarCaculationService{

	@Autowired
	private CarCaculationMapper carCaculationMapper;
	@Autowired
	private JedisClient jedisClient;
	
	@Override
	public Object getList(Integer pageNo, Integer pageSize) {
		Map<String,Object> map = new HashMap<>();
		map.put("status", 3);
		try {
			PageHelper.startPage(pageNo, pageSize);
			List<CarCaculation> list=carCaculationMapper.getCarCaculation(map);
			PageInfo<CarCaculation> info = new PageInfo<>(list);
			return new ReturnData(list,info.getPages(),(int)info.getTotal(),200,"success");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}

	@Override
	public Object addOrUpdate(String json) {
		CarCaculation cc=StringTools.json2Obj(json, CarCaculation.class);
		if(cc==null){
			return new ReturnData(201,"参数列表为空");
		}int num=1;
		Jedis jedis = null;
		if(jedis==null){
			jedis=jedisPool.getResource();
		}
		try {
			if(cc.getId()!=null){
			 num=carCaculationMapper.updateByPrimaryKeySelective(cc);
			}else{
				Map<String,Object> map = new HashMap<>();
				map.put("carType", cc.getCarType());
				map.put("status", 3);
				if(cc.getStatus()==1){//启用该规则时，将别的同类型的规则都设为禁用 启用的规则只能是一个
					List<CarCaculation> list=carCaculationMapper.getCarCaculation(map);
					for(CarCaculation c:list){
							c.setStatus(2);//禁用
							carCaculationMapper.updateByPrimaryKeySelective(c);
					}
				}
				num=	carCaculationMapper.insertSelective(cc);
			}
			if(num==0){
				return new ReturnData(202,"保存失败");
			}
			//重新将不同类型存到缓存中
			for(int i=1;i<4;i++){
				Map<String, Object> map = new HashMap<>();
				List<CarCaculation> ccmlist =null;
				map.put("carType", i);
					ccmlist = carCaculationMapper.getCarCaculation(map);// 计价规则
					JSONArray ja = JSONArray.fromObject(ccmlist);
					String jlist=ja.toString();
					jedis.set("carCaculation_carType"+i, jlist);
			}
			return new ReturnData(200,"保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}finally {
			 if(jedis!=null){
				 jedis.close();
			 }
		}
	}

	@Autowired
	private JedisPool jedisPool; 
	@Override
	public Object del(String ids) {
		Jedis jedis=null;
		if(jedis==null){
			jedis=jedisPool.getResource();
		}
		try {
			boolean flag=true;
			String[] idslist = ids.split(",");
			for(String str:idslist){
				int n = carCaculationMapper.deleteByPrimaryKey(Long.parseLong(str));
				if(n==0){
					flag=false;
					break;
				}
			}
			if(!flag){
				return new ReturnData(201,"删除失败");
			}
			//重新将不同类型存到缓存中
			for(int i=1;i<4;i++){
				Map<String, Object> map = new HashMap<>();
				List<CarCaculation> ccmlist =null;
				map.put("carType", i);
				String carcresult=jedis.get("carCaculation_carType"+i);
				if(StringTools.isEmpty(carcresult)){
					ccmlist = carCaculationMapper.getCarCaculation(map);// 计价规则
					JSONArray ja = JSONArray.fromObject(ccmlist);
					String jlist=ja.toString();
					jedis.set("carCaculation_carType"+i, jlist);
				}
			}
			
			return new ReturnData(200,"删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}finally {
			if(jedis!=null){
				jedis.close();
			}
		}
	}

	@Override
	public Object changeStatus(String json) {
		CarCaculation cc=StringTools.json2Obj(json, CarCaculation.class);
		if(cc==null){
			return new ReturnData(201,"参数列表为空");
		}
		Map<String,Object> map = new HashMap<>();
		map.put("carType", cc.getCarType());
		map.put("status", 3);boolean flag=true;
		try {
			if(cc.getStatus()==1){//启用该规则时，将别的
				List<CarCaculation> list=carCaculationMapper.getCarCaculation(map);
				for(CarCaculation c:list){
					if(c.getId().equals(cc.getId())){
						c.setStatus(cc.getStatus());
						int num=carCaculationMapper.updateByPrimaryKeySelective(c);
						if(num==0){
							flag=false;
							break;
						}
					}else{
						c.setStatus(2);//禁用
						int num=carCaculationMapper.updateByPrimaryKeySelective(c);
						if(num==0){
							flag=false;
							break;
						}
					}
				}
			}else{
				int num=carCaculationMapper.updateByPrimaryKeySelective(cc);
				if(num==0){
					flag=false;
				}
			}
			if(!flag){
				return new ReturnData(202,"Fail to update information");
			}
			return new ReturnData(200,"Fail to registered rongcloud");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
		
	}

}
